/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Janio Luis Antunes
 * @author Marcel Vinicius Kohls
 * @see http://code.google.com/p/sysdesk-java/
 *
 */
public class Utils {

    //contructor da classe
    public Utils(){
    }

    /**
     * Exibe memnsagem no formato About
     * @param strDescricao strAutor strData strVersao
     */
    public void mensagemSobre(String strDescricao, String strAutor, String strData, String strVersao){
        JFrame jfrSobre=new JFrame("Sobre");

        JOptionPane.showMessageDialog(jfrSobre,
            "Sobre: \n"+strDescricao+"\n"+"\n Versao: "+strVersao+"\n Autor: "+strAutor+"\n "+strData,
            "Sobre",
            JOptionPane.INFORMATION_MESSAGE,
            new ImageIcon(getClass().getClassLoader().getResource("Icones/sobre_32x32.png"))
            );
    }

    /**
     *
     * @param strMensagem
     * @return Retorna um Inteiro indicando a opcao que foi escolhida, 0=sim, 1=nao, 3=cancelar
     */
    public int mensagemSimNaoCancela(String strMensagem){
        JFrame jfrSair=new JFrame("Sair");

        //Custom button text
        Object[] options = {"Sim", "Não", "Cancelar"};

        int n = JOptionPane.showOptionDialog(jfrSair,
            strMensagem,
            "Sair",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[2]);

        return n;
    };

    /**
     * 
     * @return Retorna a coluna máxima para a tela passada como parametro
     */
    public int getTelaColunaMaxima(JFrame jfrOrigem){
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        int w = jfrOrigem.getSize().width;
        return (dim.width-w);
    }

    /**
     *
     * @return Retorna a coluna central para a tela passada como parametro
     */
    public int getTelaColunaCentro(JFrame jfrOrigem){
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        int w = jfrOrigem.getSize().width;
        return (dim.width-w)/2;
    }

    /**
     *
     * @return Retorna a linha maxima para a tela passada como parametro
     */
    public int getTelaLinhaMaxima(JFrame jfrOrigem){
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        int h = jfrOrigem.getSize().height;
        return (dim.height-h)-24;
    }

    /**
     *
     * @return Retorna a linha central para a tela passada como parametro
     */
    public int getTelaLinhaCentro(JFrame jfrOrigem){
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        int h = jfrOrigem.getSize().height;
        return (dim.height-h)/2;
    }

    /**
     * @param valorFloat
     * @return Retorna um valor float formatado em string compontuacao 0.00
     */
    public String floatToStr(float valorFloat){
       DecimalFormat decFormat = new DecimalFormat("#,##0.00");

       return decFormat.format(valorFloat);
    }

    /**
     * date validation using SimpleDateFormat it will take a string and make sure it's in the proper format as defined by you, and it will also make sure that it's a legal date
     * @author iamfyrestorm
     * @see http://www.dreamincode.net/forums/topic/14886-date-validation-using-simpledateformat/
     */

    public boolean isValidDate(String date) throws Exception {

        // set date format, this can be changed to whatever format
        // you want, MM-dd-yyyy, MM.dd.yyyy, dd.MM.yyyy etc.
        // you can read more about it here:
        // http://java.sun.com/j2se/1.4.2/docs/api/index.html

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Boolean bl_Resultado = true;

        // declare and initialize testDate variable, this is what will hold
        // our converted string

        Date testDate = new Date();

        // we will now try to parse the string into date form
        try
        {
          testDate = sdf.parse(date);
        }

        // if the format of the string provided doesn't match the format we
        // declared in SimpleDateFormat() we will get an exception

        catch (ParseException e)
        {
          bl_Resultado = false;
          throw new Exception("Formato da data deve ser dd/mm/aaaa.");
        }

        // dateformat.parse will accept any date as long as it's in the format
        // you defined, it simply rolls dates over, for example, december 32
        // becomes jan 1 and december 0 becomes november 30
        // This statement will make sure that once the string
        // has been checked for proper formatting that the date is still the
        // date that was entered, if it's not, we assume that the date is invalid

        if (!sdf.format(testDate).equals(date))
        {
            bl_Resultado = false;
            throw new Exception("Data inválida.");
        }

        // if we make it to here without getting an error it is assumed that
        // the date was a valid one and that it's in the proper format

        return bl_Resultado;

    } // end isValidDate


}
